//
//  ASDetailTopView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/30.
//

#import "ASDetailTopView.h"

@implementation ASDetailTopView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.leftBtn];
        [self addSubview:self.rightBtn];
        [self addSubview:self.borderView];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.right.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(self.leftBtn.mas_right);
        make.width.mas_equalTo(self.leftBtn.mas_width);
    }];
    [self.borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (UIButton *)leftBtn{
    if (!_leftBtn) {
        _leftBtn = [UIButton new];
        [_leftBtn setBackgroundImage:[ASTool createImageWithColor:[UIColor colorWithHexString:@"ffffff"]] forState:UIControlStateNormal];
        [_leftBtn setBackgroundImage:[ASTool createImageWithColor:[UIColor colorWithHexString:@"#FF7785"]] forState:UIControlStateSelected];
        _leftBtn.selected = YES;
        _leftBtn.tag = 0;
        [_leftBtn setTitle:@"方形" forState:UIControlStateNormal];
        [_leftBtn setTitleColor:[UIColor colorWithHexString:@"FF7785"] forState:UIControlStateNormal];
        [_leftBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateSelected];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_leftBtn addTarget:self action:@selector(clickSubBtn:) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.adjustsImageWhenHighlighted = NO;
        ///设置半边圆角
        _leftBtn.frame = CGRectMake(0, 0, 75, 40);
        [ASTool addRoundRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft) cornerRadii:CGSizeMake(20, 20) subView:_leftBtn];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton new];
        [_rightBtn setBackgroundImage:[ASTool createImageWithColor:[UIColor colorWithHexString:@"ffffff"]] forState:UIControlStateNormal];
        [_rightBtn setBackgroundImage:[ASTool createImageWithColor:[UIColor colorWithHexString:@"#FF7785"]] forState:UIControlStateSelected];
        _rightBtn.selected = NO;
        _rightBtn.tag = 1;
        [_rightBtn setTitle:@"圆形" forState:UIControlStateNormal];
        [_rightBtn setTitleColor:[UIColor colorWithHexString:@"FF7785"] forState:UIControlStateNormal];
        [_rightBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateSelected];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _rightBtn.adjustsImageWhenHighlighted = NO;
        [_rightBtn addTarget:self action:@selector(clickSubBtn:) forControlEvents:UIControlEventTouchUpInside];
        ///设置半边圆角
        _rightBtn.frame = CGRectMake(75, 0, 75, 40);
        [ASTool addRoundRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight) cornerRadii:CGSizeMake(20, 20) subView:_rightBtn];

    }
    return _rightBtn;
}

- (UIView *)borderView{
    if (!_borderView) {
        _borderView = [UIView new];
        [_borderView setBackgroundColor:[UIColor clearColor]];
        _borderView.userInteractionEnabled = NO;
        _borderView.layer.masksToBounds = YES;
        _borderView.layer.cornerRadius = 20;
        _borderView.layer.borderColor = [UIColor colorWithHexString:@"#FF7785"].CGColor;
        _borderView.layer.borderWidth = 1.f;
    }
    return _borderView;
}

- (void)clickSubBtn:(UIButton *)btn{
    if (btn.tag == self.leftBtn.tag) {
        self.leftBtn.selected = YES;
        self.rightBtn.selected = NO;
    }else{
        self.leftBtn.selected = NO;
        self.rightBtn.selected = YES;
    }
    if (self.clickSubBtnBlock) {
        self.clickSubBtnBlock(btn.tag);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
